from datetime import timedelta

T = int(input())
import datetime

init_date = datetime.datetime(year=1970, day=1, month=1)
for _ in range(T):
    date, time, between = input().split()
    dt = datetime.datetime.strptime(f"{date} {time}", "%Y-%m-%d %H:%M:%S")
    between = int(between)
    n = (dt - init_date) // timedelta(minutes=between)
    print(init_date + timedelta(minutes=between) * n)
